﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Dropit.BlogPageProvider.Blog;

namespace Dropit.BlogPageProvider
{
	public class BlogService
	{
		private static BlogService _instance;
		private static object _instanceLock = new object();
		private Dictionary<string, IBlogProvider> _providers;

		private BlogService()
		{
			_providers = new Dictionary<string, IBlogProvider>();
			Configuration.BlogPageProviderConfiguration conf = (Configuration.BlogPageProviderConfiguration)ConfigurationManager.GetSection("blogPageProvider");
			foreach (Configuration.Provider provider in conf.Providers)
			{
				string[] type = provider.Type.Split(',');
				if (type.Length == 2)
				{
					_providers.Add(provider.Name, (IBlogProvider)Activator.CreateInstance(type[1], type[0]).Unwrap());
				}
			}
		}

		public List<string> Providers
		{
			get { return _providers.Keys.ToList(); }
		}

		public static BlogService Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLock)
					{
						if (_instance == null)
						{
							_instance = new BlogService();
						}
					}
				}
				return _instance;
			}
		}

		public List<IBlog> GetBlogs(Connection connection)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetBlogs(connection);
				}
			}
			catch
			{
			}
			return new List<IBlog>(0);
		}

		public IBlog GetBlogById(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetBlogById(connection, id);
				}
			}
			catch
			{
			}
			return null;
		}

		public IComment GetCommentById(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetCommentById(connection, id);
				}
			}
			catch
			{
			}
			return null;
		}

		public List<IComment> GetComments(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetComments(connection, id);
				}
			}
			catch
			{
			}
			return new List<IComment>(0);
		}

		public IPost GetPostById(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetPostById(connection, id);
				}
			}
			catch
			{
			}
			return null;
		}

		public List<IPost> GetPosts(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.GetPosts(connection, id);
				}
			}
			catch
			{
			}
			return new List<IPost>(0);
		}

		public IPost CreatePost(Connection connection)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.CreatePost(connection);
				}
			}
			catch
			{
			}
			return null;
		}

		public IComment CreateComment(Connection connection)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.CreateComment(connection);
				}
			}
			catch
			{
			}
			return null;
		}

		public string SavePost(Connection connection, string parentId, IPost post)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.SavePost(connection, parentId, post);
				}
			}
			catch
			{
			}
			return string.Empty;
		}

		public void UpdatePost(Connection connection, IPost post)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					provider.UpdatePost(connection, post);
				}
			}
			catch
			{
			}
		}

		public string SaveComment(Connection connection, string parentId, IComment comment)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					return provider.SaveComment(connection, parentId, comment);
				}
			}
			catch
			{
			}
			return string.Empty;
		}

		public void UpdateComment(Connection connection, IComment comment)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					provider.UpdateComment(connection, comment);
				}
			}
			catch
			{
			}
		}

		public void DeletePost(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					provider.DeletePost(connection, id);
				}
			}
			catch
			{
			}
		}

		public void DeleteComment(Connection connection, string id)
		{
			try
			{
				IBlogProvider provider = GetProvider(connection);
				if (provider != null)
				{
					provider.DeleteComment(connection, id);
				}
			}
			catch
			{
			}
		}

		private IBlogProvider GetProvider(Connection connection)
		{
			try
			{
				if (_providers.ContainsKey(connection.ProviderName))
				{
					return _providers[connection.ProviderName];
				}
			}
			catch
			{
			}
			return null;
		}
	}
}